using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BBCoder
{
    public partial class frmPickTemplate : Form
    {
        public frmPickTemplate()
        {
            InitializeComponent();
        }

        public string pickTemplateText = "";

        private void frmPickTemplate_Load(object sender, EventArgs e)
        {
            try
            {
                if (!System.IO.Directory.Exists("./Templates/"))
                {
                    MessageBox.Show("Could not locate Templates folder.", "Error");
                    this.Close();
                }

                System.IO.FileInfo[] fileDir = new System.IO.DirectoryInfo("./Templates/").GetFiles();
                string[] filePaths = new string[fileDir.Length];

                int fileCounter = 0;
                foreach (System.IO.FileInfo file in fileDir)
                {
                    filePaths[fileCounter] = file.FullName;
                    fileCounter++;
                }

                int pathCounter = 0;
                foreach (string str in filePaths)
                {
                    string[] tmpInfo = getTemplateInfo(str);

                    lstTemplate.Items.Add(tmpInfo[0]);
                    lstTemplate.Items[pathCounter].Tag = tmpInfo;
                    lstTemplate.Items[pathCounter].ImageIndex = 0;
                    pathCounter++;
                }
            }
            catch { }
        }

        private string[] getTemplateInfo(string path)
        {
            string[] tmpResult = { "" , "", "" };
            string tmpRead = System.IO.File.ReadAllText(path);

            tmpResult[0] = tmpRead.Substring(tmpRead.IndexOf("[title]") 
                + 7, tmpRead.IndexOf("[/title]") - 
                (tmpRead.IndexOf("[title]") + 7));

            tmpResult[1] = tmpRead.Substring(
                tmpRead.IndexOf("[description]") + 13,
                tmpRead.IndexOf("[/description]") - 
                (tmpRead.IndexOf("[description]") + 13));

            tmpResult[2] = tmpRead.Substring(
                tmpRead.IndexOf("[body]") + 6,
                tmpRead.LastIndexOf("[/body]") -
                (tmpRead.IndexOf("[body]") + 6));

            return tmpResult;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (pickTemplateText.Length == 0)
            {
                MessageBox.Show("Please select an item from the list.");               
                return;
            }           
        }

        private void lstTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTemplate.SelectedItems.Count == 0)
            {
							btnOpen.DialogResult = DialogResult.None;

                lblTitle.Text = String.Empty;
                lblDescription.Text = String.Empty;
                pickTemplateText = String.Empty;
                return;
            }
            string[] templateInfo = (string[])lstTemplate.SelectedItems[0].Tag;
            lblTitle.Text = templateInfo[0];
            lblDescription.Text = templateInfo[1];

            pickTemplateText = templateInfo[2];
            SendKeys.Send("{TAB}");

						btnOpen.DialogResult = DialogResult.OK;
        }
    }
}